package cn.signalpha.modules.social.controller.api;

import cn.signalpha.common.annotation.FormRepeatCommitAnnotation;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.social.dto.LikesDTO;
import cn.signalpha.modules.social.entity.Comment;
import cn.signalpha.modules.social.entity.Likes;
import cn.signalpha.modules.social.entity.Topic;
import cn.signalpha.modules.social.service.CommentService;
import cn.signalpha.modules.social.service.LikesService;
import cn.signalpha.modules.social.service.TopicService;
import cn.signalpha.modules.social.vo.LikesVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SOCIAL_NAME + "/wap/likes")
@Api(value = "实体点赞", tags = "实体点赞")
public class ApiLikesController extends BaseController {

	private final UserService userService;
	private final TopicService topicService;
	private final LikesService likesService;
	private final CommentService commentService;
	private final ApplicationEventPublisher eventPublisher;

	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "我的点赞", notes = "")
	public R<IPage<LikesVO>> myLikesPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("targetType", "1");
		params.put("userId", getUser().getUserId().toString());
		return R.data(likesService.selectLikesPage(Condition.getPage(query), params));
	}

	@PostMapping("/save")
	@FormRepeatCommitAnnotation(intervalSecond = 1)
	@ApiOperation(value = "点赞", notes = "返回点赞id")
	public R save(@Valid @RequestBody LikesDTO likesDTO) {
		Long userId = getUser().getUserId();
		Long toUserId = 0L;
		Likes likes = likesService.getByUserIdAndTarget(userId, likesDTO.getTargetId(), likesDTO.getTargetType());
		if(ObjectUtil.isEmpty(likes)){
			if(likesDTO.getTargetType() == 1){
				Topic topic = topicService.getById(likesDTO.getTargetId());
				Assert.isNull(topic, MsgConstant.OBJECT_NULL);
				topicService.updateById(Topic.builder().id(topic.getId()).likeNum(topic.getLikeNum() + 1).build());
				if(topic.getUserId() > 0){
					toUserId = topic.getUserId();
				}
			}else if(likesDTO.getTargetType() == 2){
				Comment comment = commentService.getById(likesDTO.getTargetId());
				Assert.isNull(comment, MsgConstant.OBJECT_NULL);
				commentService.updateById(Comment.builder().id(comment.getId()).likeNum(comment.getLikeNum() + 1).build());
				if(comment.getFromUserId() > 0){
					toUserId = comment.getFromUserId();
				}
			}

			likes = new Likes();
			likes.setUserId(userId);
			likes.setToUserId(toUserId);
			likes.setTargetId(likesDTO.getTargetId());
			likes.setTargetType(likesDTO.getTargetType());
			likesService.save(likes);
//			eventPublisher.publishEvent(new NoticeEvent(2, userId, likes.getId(), NoticeEntityTypeEnum.LIKES.getKey()));
		}
		return R.data(likes.getId());
	}

	@PostMapping("/remove")
	@FormRepeatCommitAnnotation(intervalSecond = 1)
	@ApiOperation(value = "删除点赞", notes = "传入id")
	public R remove(@Valid @RequestBody IdDTO idDTO) {
		Likes likes = likesService.getById(idDTO.getId());
		Assert.isNull(likes, MsgConstant.OBJECT_NULL);
		if(likes.getTargetType() == 1){
			Topic topic = topicService.getById(likes.getTargetId());
			Assert.isNull(topic, MsgConstant.OBJECT_NULL);
			topicService.updateById(Topic.builder().id(topic.getId()).likeNum(topic.getLikeNum() - 1).build());
		}else if(likes.getTargetType() == 2){
			Comment comment = commentService.getById(likes.getTargetId());
			Assert.isNull(comment, MsgConstant.OBJECT_NULL);
			commentService.updateById(Comment.builder().id(comment.getId()).likeNum(comment.getLikeNum() - 1).build());
		}
		return R.status(likesService.removeById(idDTO.getId()));
	}

}
